<template>
	<!-- #ifdef MP -->
	<view class="btn_wraps">
		<image src="@/static/images/add.png" mode="widthFix"
			v-if="meth == 0 && (curRoute.indexOf('pMine/infoEdit') == -1 || curRoute.indexOf('pMine/matchingYes/apply') == -1)"
			@click="addOrg"></image>
		<!-- <image v-if="indexs" src="@/static/images/home.png" mode="widthFix" @click="$gourl('/pages/index/index',2)"> -->
		<image v-if="indexs" :src="imgUrl + 'gif2.gif'" mode="widthFix" @click="$gourl('/pages/index/index',2)"
			style="opacity: .8;">
		</image>
		<u-popup v-model="maskShow" mode="center" border-radius="10">
			<view class="mask_box">
				<view class="top_img">
					<image src="@/static/images/mine/mine_maks_bg.png" mode="widthFix"></image>
				</view>
				<view class="text">您还没有登录，是否进行授权登录</view>
				<view class="tn_box">
					<view>
						<!-- <view class="button" @click="()=>{$gourl('/pIndex/login');maskShow = false}">授权登录</view> -->
						<view class="button" @click="wxGetUserInfo">授权登录</view>
						<view class="texts" @click="maskShow = false">以后再说</view>
					</view>
				</view>
			</view>
		</u-popup>
		<u-popup mode="center" v-model="noBindShow" border-radius="30" closeable>
			<view class="yes_bind_box">
				<view class="big_text">
					<image src="@/static/images/pMine/text2.png" mode="widthFix"></image>
				</view>
				<view class="small_box">成功加入组织即可领取官方专属名片</view>
				<view class="yes_btn">
					<image src="@/static/images/pMine/yesbtn.png" mode="widthFix"
						@click="gourls('/pMine/matchingYes/editName?type=agree')"></image>
				</view>
				<view class="yes_btn">
					<image src="@/static/images/pMine/nobtn.png" mode="widthFix"
						@click="gourls('/pMine/matchingYes/index?type=shenqing')"></image>
				</view>
			</view>
		</u-popup>
		<u-popup mode="center" v-model="yesBindShow" border-radius="30" closeable :mask-close-able="false"
			@close="close">
			<view class="yes_bind_box">
				<view class="big_text">
					<image src="@/static/images/pMine/text.png" mode="widthFix"></image>
				</view>
				<view class="small_box">请核对您在组织内预留的个人信息</view>
				<view class="user_content">
					<view class="imgcss" :style="'background: url('+info.member.character.avatar+') no-repeat'">
					</view>
					<view class="user_info">
						<view class="user_name">
							<text class="name">{{info.member.character.name}}</text>
							<text class="position" v-for="(k, i) in info.member.position" :key="i"
								v-if="info.member.position && i<1">
								{{ k.name }}&nbsp;
							</text>
						</view>
						<view class="phone">预留电话：{{info.member.character.phone}}</view>
					</view>
				</view>

				<view class="button" @click="gourls('/pMine/matchingYes/index?type=receive',3)">核对个人信息</view>
				<view class="small_button" @click="close()">暂不核对</view>
			</view>
		</u-popup>
		<u-popup mode="bottom" v-model="phoneShow">
			<view style="padding: 30rpx;">
				<view class="get_phone_info">
					<view>
						<image :src="infoData.logo" mode="widthFix"></image>
					</view>
					<view style="margin-top: 50rpx;">{{infoData.name}}</view>
					<view class="content" style="display: flex;align-items: center;justify-content: center;">
						<view>
							<view>申请获取以下权限</view>
							<text>获取您微信绑定的手机号，<text style="color: #888;">用于注册或登录小程序以识别您的身份</text></text>
						</view>
					</view>
				</view>
				<button class="get_phone_button" style="margin-top: 30rpx;" open-type="getPhoneNumber"
					@getphonenumber="getPhoneNumber">授权微信手机号</button>
			</view>
		</u-popup>
	</view>
	<!-- #endif -->
</template>
<script>
	export default {
		props: ['keyss'],
		data() {
			return {
				indexs: false,
				meth: 0,
				infoData: JSON.parse(uni.getStorageSync('orgInfo')) ? JSON.parse(uni.getStorageSync('orgInfo')) : '',
				maskShow: false,
				noBindShow: false,
				yesBindShow: false,
				info: null,
				imgUrl: this.$imgUrl,
				curRoute: '',
				phoneShow: false,
				loginResCode: '',
				loginType: '',
			}
		},
		onShow() {},
		created() {
			let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
			let curRoute = routes[routes.length - 1].route // 获取当前页面路由，也就是最后一个打开的页面路由
			this.curRoute = curRoute
			console.log(curRoute);
			if (curRoute.indexOf('pages/index') !== -1) {
				this.indexs = false
			} else {
				this.indexs = true
			}
			if (uni.getStorageSync('userInfo')) {
				if (uni.getStorageSync('userInfo').member && uni.getStorageSync('userInfo').member.method) {
					this.meth = uni.getStorageSync('userInfo').member.method
					this.info = uni.getStorageSync('userInfo')
				} else {
					this.meth = 0
				}
				if (this.meth !== 1) {
					this.$allApi.getUserInfo().then(res => {
						this.info = res.data
						if (res.data.member) {
							this.meth = this.info.member.method
						}
						uni.setStorageSync('userInfo', res.data)
					})
				}
				this.loginType == 'type'
				// this.addOrg()
			} else {
				this.meth = 0
			}
		},
		methods: {
			getPhoneNumber(e) {
				console.log(this.loginType);
				let _this = this
				if (!e.detail.code) {
					this.$u.toast('授权已拒绝,登陆失败！')
					this.phoneShow = false
					this.maskShow = false
					// setTimeout(() => {
					// 	this.$backTo(1)
					// }, 1000)
					return
				} else {
					_this.$allApi.appletAuth({
						code: _this.loginResCode,
						phone_code: e.detail.code,
						signature: _this.wxInfo.signature,
						rawData: _this.wxInfo.rawData,
						encryptedData: _this.wxInfo.encryptedData,
						iv: _this.wxInfo.iv
					}).then(res => {
						this.show = false
						uni.setStorageSync('appToken', res.data.token)
						this.$u.toast('登陆成功')
						if (res.data.isClaim == 1) {
							this.phoneShow = false
							this.maskShow = false
							this.$emit('loginYes')
							// setTimeout(() => {
							// 	this.$backTo(1)
							// }, 1000)
						} else {
							uni.showModal({
								title: res.data.isClaim
							})
							this.phoneShow = false
							this.maskShow = false
							if (res.data.member) {
								uni.setStorageSync('bindYes', 'yes')
								if (this.loginType == 'logins') {
									this.$emit('loginYes')
								} else {
									if (curRoute.indexOf('pages/index/mine') !== -1) {
										this.$emit('loginYes')
									} else {
										setTimeout(() => {
											this.$gourl('/pages/index/mine', 2)
										}, 1000)
									}
								}
							} else {
								uni.setStorageSync('bindYes', 'no')
								if (this.loginType == 'logins') {
									this.$emit('loginYes')
								} else {
									if (curRoute.indexOf('pages/index/mine') !== -1) {
										this.$emit('loginYes')
									} else {
										setTimeout(() => {
											this.$gourl('/pages/index/mine', 2)
										}, 1000)
									}
								}
							}
						}

					}).catch(res => {
						this.phoneShow = false
						this.maskShow = false
					})
				}

			},
			wxGetUserInfo(code) {
				let _this = this;
				uni.getUserProfile({
					desc: '获取您的昵称、头像、地区及性别', // 这里的desc在微信开发文档中要求必须填写的
					lang: 'zh_CN',
					provider: 'weixin',
					success: (res) => {
						console.log(res);
						_this.wxInfo = res
						_this.phoneShow = true
						_this.maskShow = false
					},
					fail: (res) => {
						this.$u.toast('授权已拒绝,登陆失败！')
						_this.phoneShow = true
						_this.maskShow = false
					}
				})
			},
			gourls(url, type) {
				this.yesBindShow = false
				this.noBindShow = false
				setTimeout(() => {
					this.$gourl(url)
				}, 500)
			},
			close() {
				this.yesBindShow = false
				this.noBindShow = false
			},
			addOrg(type) {
				console.log(123);
				this.loginType = type
				if (!uni.getStorageSync('appToken')) {
					this.maskShow = true
					let _this = this
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							let code = loginRes.code;
							_this.loginResCode = code

						}
					});
					return
				}
				this.$allApi.checkMemberExistence().then(res => {
					if (res.data) {
						if (res.data.status == 1) {
							if (res.data.is_show == 0) {
								this.yesBindShow = true
							}
						} else {
							if (this.loginType !== 'type') {
								uni.setStorageSync('apply', JSON.stringify(res.data))
								this.$gourl('/pMine/matchingYes/apply?status=' + res.data.status +
									'&reason=' + res.data.reason)
							}
						}
					} else {
						if (this.loginType !== 'type' && this.loginType !== 'logins') {
							this.noBindShow = true
						}
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.get_phone_info {
		text-align: center;
		font-size: 36rpx;

		image {
			width: 180rpx;
			height: 180rpx;
		}

		.content {
			margin-top: 30rpx;
			font-size: 28rpx;
			text-align: left;
		}
	}

	.get_phone_button {
		width: 630rpx;
		height: 88rpx;
		background: linear-gradient(to right, #1c91fb 0%, #293ffc 100%);
		border-radius: 44rpx;
		box-shadow: -4rpx 0rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		font-size: 28rpx;
	}

	.get_phone_button::after {
		border: initial !important;
	}

	.yes_bind_box {
		width: 640rpx;
		background: #ffffff;
		border-radius: 30rpx;
		background: url('@/static/images/pMine/bg.png') no-repeat center;
		background-size: 100% 100%;
		padding: 30rpx 60rpx;


		.big_text {
			display: flex;
			align-items: center;
			justify-content: center;
			padding-top: 30rpx;

			image {
				width: 320rpx;
			}
		}

		.small_box {
			width: 520rpx;
			height: 60rpx;
			background: linear-gradient(0deg, rgba(97, 96, 254, 0.20) 0%, rgba(0, 145, 255, 0.20) 100%);
			border-radius: 30rpx;
			box-shadow: -2rpx 0rpx 4rpx 2rpx rgba(22, 0, 50, 0.14);
			text-align: center;
			line-height: 60rpx;
			margin: 0 auto;
			color: #467DFE;
			margin-top: 30rpx;
		}

		.yes_btn {
			width: 520rpx;
			margin-top: 30rpx;

			image {
				width: 520rpx;
				height: 172rpx;
			}
		}

		.small_text {
			text-align: center;
			color: #467DFE;
			margin-top: 60rpx;
			padding-bottom: 30rpx;
			border-bottom: 1px solid #9EC5FF;
		}

		.user_content {
			margin-top: 30rpx;
			padding: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			background: #ffffff;
			border-radius: 10rpx;
			box-shadow: -4rpx 0rpx 8rpx 4rpx rgba(0, 15, 151, 0.08);
		}

		.imgcss {
			width: 120rpx;
			height: 120rpx;
			border-radius: 60rpx;
			background-position: top center;
			background-size: 100% auto !important;
		}

		.user_info {
			flex: 1;
			padding-left: 25rpx;

			.user_name {
				display: flex;
				align-items: center;

				.name {
					font-size: 28rpx;
				}

				.position {
					font-size: 18rpx;
					padding: 4rpx 14rpx;
					background: linear-gradient(-30deg, #FFCF66, #FFE2A3);
					border-radius: 6rpx;
					color: #333333;
					margin-left: 10rpx;
				}
			}

			.phone {
				color: #888888;
				font-size: 24rpx;
				margin-top: 14rpx;
			}
		}

		.button {
			width: 420rpx;
			height: 88rpx;
			background: linear-gradient(0deg, #1c91fb 0%, #293ffc 100%);
			border-radius: 44rpx;
			box-shadow: -4rpx 0rpx 10.8rpx 0.4rpx rgba(0, 19, 132, 0.24), 0rpx 0rpx 8.64rpx 0rpx rgba(255, 255, 255, 0.34) inset, -1.81rpx -0.85rpx 76rpx 2rpx rgba(149, 0, 255, 0.49) inset, 10.18rpx 6.36rpx 16rpx 12rpx rgba(0, 192, 255, 0.93) inset;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			margin: 0 auto;
			margin-top: 60rpx;
		}

		.small_button {
			color: #ccc;
			text-align: center;
			margin-top: 30rpx;
		}
	}

	.receive_box {
		height: 100vh;
		width: 100vw;
		position: relative;

		.img1 {
			width: 534rpx;
			height: 325rpx;
			position: absolute;
			right: 110rpx;
			top: 524rpx;
		}
	}


	.small_text {
		text-align: center;
		color: #467DFE;
		margin-top: 60rpx;
		padding-bottom: 30rpx;
		border-bottom: 1px solid #9EC5FF;
	}

	.user_content {
		margin-top: 30rpx;
		padding: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: #ffffff;
		border-radius: 10rpx;
		box-shadow: -4rpx 0rpx 8rpx 4rpx rgba(0, 15, 151, 0.08);
	}

	.imgcss {
		width: 120rpx;
		height: 120rpx;
		border-radius: 60rpx;
		background-position: top center;
		background-size: 100% auto !important;
	}

	// .user_info {
	// 	flex: 1;
	// 	padding-left: 25rpx;

	// 	.user_name {
	// 		font-size: 36rpx;

	// 		text {
	// 			font-size: 18rpx;
	// 			padding: 4rpx 14rpx;
	// 			background: linear-gradient(-30deg, #FFCF66, #FFE2A3);
	// 			border-radius: 6rpx;
	// 			color: #333333;
	// 			margin-left: 10rpx;
	// 		}
	// 	}

	// 	.phone {
	// 		color: #888888;
	// 		margin-top: 14rpx;
	// 	}
	// }

	.btn_wraps {
		position: fixed;
		right: 30rpx;
		bottom: 20vh;
		width: 102rpx;
		z-index: 999;

		image {
			width: 102rpx;
			height: 102rpx;
		}
	}

	.mask_box {
		width: 630rpx;
		padding-bottom: 30rpx;

		image {
			width: 630rpx;
		}

		.text {
			margin-top: 40rpx;
			text-align: center;
			font-size: 28rpx;
			color: #1781F7;
		}

		.tn_box {
			display: flex;
			align-items: center;
			justify-content: center;

			.button {
				width: 540rpx;
				height: 68rpx;
				background: linear-gradient(to right, #1c91fb 0%, #293ffc 100%);
				border-radius: 34rpx;
				display: flex;
				margin-top: 60rpx;
				align-items: center;
				justify-content: center;
				color: #fff;
			}

			.texts {
				font-size: 28rpx;
				font-weight: 400;
				text-align: center;
				margin-top: 30rpx;
				color: #888888;
			}
		}

	}
</style>
